<template>
    <div
        class="flex justify-center items-center w-full h-[100vh] bg-white"
        element-loading-background="rgba(122, 122, 122, 0.2)"
        v-loading="loginStatus == 0"
    >
        <el-result
            icon="success"
            title="微信登录成功"
            v-if="loginStatus == 1"
        />
        <el-result icon="error" title="微信登录失败" v-if="loginStatus == 2" />
    </div>
</template>

<script lang="ts" setup>
import { ChannelEnum } from "@/enums/channelEnum";
import { wxScanLogin } from "@/api/login";
import useUserStore from "@/stores/user";

definePageMeta({
    layout: false,
    middleware: ["login"],
});

const loginStatus = ref(0);

const route = useRoute();

const wxLogin = (code) => {
    wxScanLogin({
        code: code,
        channel: ChannelEnum.PC,
    })
        .then((res) => {
            loginStatus.value = 1;
            useUserStore().login(res.token);
        })
        .catch((err) => {
            loginStatus.value = 2;
        });
};

onMounted(() => {
    let code = route.query.code;
    if (code) wxLogin(code);
});
</script>
<style lang="scss" scoped></style>
